import { DeviceManager } from './../../../../../common/mpj-deviceCenter/src/deviceManager';
import { PrintService } from './../../../../../service/print.service';
import { Component, Input, OnInit, EventEmitter, Output, ChangeDetectorRef } from '@angular/core';
import { PrintComponentService } from "../printcontainer.component";

export class TestPrintService {
    static current: TestPrintService = new TestPrintService();

    private testTemplate: TestPrintComponent;

    regist(testTemplate: TestPrintComponent) {
        this.testTemplate = testTemplate;
    }

    clearData(){
        this.testTemplate.store = null;
    }

    bindData(bindData: {
        auxPrintTestDevice;
        store;
        token;
    }) {
        this.testTemplate.auxPrintTestDevice = bindData.auxPrintTestDevice;
        this.testTemplate.token = bindData.token;
        this.testTemplate.store = bindData.store;
    }
}

@Component({
    selector: 'test-print',
    template: `
        <div style="position:fixed;top:10000px;">
            <div fxLayout="column" class="print-receipt print-receipt-test" [ngClass]='auxPrintTestDevice.paperSize'>
                <p></p>
                <p><span>恭喜，打印机已经连接！</span></p>
                <p></p>
                <p><span>选择纸型：</span><span *ngIf="auxPrintTestDevice.paperSize">{{auxPrintTestDevice.paperSize}}</span></p>
                <p><span>请检查下面的横线是否为整行，如果未撑满则选择打印机宽度过小，折行则表明选择打印机宽度过宽。</span></p>
                <hr/>
                <div *ngIf='auxPrintTestDevice.autoCut'>
                    <p><span>已选择自动切纸</span></p>
                    <p><span>打印的测试单应当被切开，如果未切开，表明你的打印机不支持自动切纸。</span></p>
                </div>
            </div>
            <div dataBindSpy (dataBindFinish)="dataBindFinish()" *ngIf="store"></div>
        </div>
    `,
    styleUrls: ["./../../printer.component.scss"]
})
export class TestPrintComponent implements OnInit {
    constructor(
        private printSvr: PrintService,
        private ref: ChangeDetectorRef
    ) {
        TestPrintService.current.regist(this);
    }
    printerManager;
    adsServer;
    auxPrintTestDevice = <any>{};
    store;
    token;
    @Output() createPrintTask: EventEmitter<any> = new EventEmitter<any>();
    ngOnInit() {
        this.printerManager = DeviceManager.Current.getManagerByType("printerManager");
    }

    dataBindFinish() {
        this.printTask();
    }

    printTask() {
        let that = this;
        this.adsServer = this.printSvr.adsServer;
        if (!this.adsServer) {
            console.warn("ads服务未配置!");
            return;
        }
        let html = document.querySelector(".print-receipt-test").outerHTML;
        let printParams = {
            printMode: "http",
            printTemplate: html,
            url: this.adsServer.url + "/printer/print/html",
            mode: "text",
            deviceInfo: this.auxPrintTestDevice.deviceInfo,
            printId: "",
            type: "wifi",
            storeId: this.store.id,
            token: this.token,
            directives: {
                "auto-cut": this.auxPrintTestDevice.autoCut,
                "print-mode": "text",
                "open-cash-drawer": false,
                "paper-scroll-rows": this.auxPrintTestDevice.paperScrollRows,
                "instruction-set": this.auxPrintTestDevice.instructionSet
            }
        }
        // let result = this.printerManager.printHtml(printParams);
        that.createPrintTask.emit(printParams);
        that.ref.detectChanges();
        // that.store = null;
    }
}